﻿using System;
using IFDB.Models;
using IFDB.Models.Tables;

namespace IFDB.ViewModels.PartialViewModels
{
    public class LeagueTableItemViewModel : IComparable<LeagueTableItemViewModel>
    {
        public Team Team { get; set; }
        public short WonHome { get; set; }
        public short DrawnHome { get; set; }
        public short LostHome { get; set; }
        public short ScoredHome { get; set; }
        public short ConcededHome { get; set; }
        public short WonAway { get; set; }
        public short DrawnAway { get; set; }
        public short LostAway { get; set; }
        public short ScoredAway { get; set; }
        public short ConcededAway { get; set; }

        public short Played
        {
            get { return (short)(WonHome + DrawnHome + LostHome + WonAway + DrawnAway + LostAway); }
        }

        public short Won
        {
            get { return (short)(WonHome + WonAway); }
        }

        public short Drawn
        {
            get { return (short)(DrawnHome + DrawnAway); }
        }

        public short Lost
        {
            get { return (short)(LostHome + LostAway); }
        }

        public short Scored
        {
            get { return (short)(ScoredHome + ScoredAway); }
        }

        public short Conceded
        {
            get { return (short)(ConcededHome + ConcededAway); }
        }

        public short GoalDifference
        {
            get { return (short)(ScoredHome + ScoredAway - ConcededHome - ConcededAway); }
        }

        public short Points
        {
            get { return (short)(3 * (WonHome + WonAway) + DrawnHome + DrawnAway); }
        }

        public int CompareTo(LeagueTableItemViewModel other)
        {
            if (Points != other.Points)
                return other.Points.CompareTo(Points);

            if (Points == other.Points)
            {
                if (GoalDifference != other.GoalDifference)
                    return other.GoalDifference.CompareTo(GoalDifference);

                if (GoalDifference == other.GoalDifference)
                {
                    if (ScoredHome + ScoredAway != other.ScoredHome + other.ScoredAway)
                        return (other.ScoredHome + other.ScoredAway).CompareTo(ScoredHome + ScoredAway);
                }
            }

            return 0;
        }
    }
}
